<?php
/**
 * api controller 为了后台使用微信接口无法操作所提供
 */
namespace frontend\controllers;

use bc\controller\FrontController;
use bc\util\Util;
use bc\wx\Auth;
use bc\util\AppConst;
use bc\model\wx\JsApiModel;

class ApiController extends FrontController
{
	
	/**
	 *  生成api签名
	 */
	public function actionJsSign()
	{
		$this->layout = false;
		
		$url = $this->get('url');
		if($url)
		{
			$url = urldecode($url);
			
			$auth = new Auth($this->wechat()->appid, $this->wechat()->secret);
			$accessToken = $auth->accessToken;
			
			$jsApiModel = new JsApiModel();
			$ticket = $jsApiModel->getTicket($accessToken);
			
			$time = time();
			
			$noncestr = $this->createNonceStr();
			
			$str  = 'jsapi_ticket='.$ticket;
			$str .= '&noncestr='.$noncestr;
			$str .= '&timestamp='.$time;
			$str .= '&url='.$url;
			
			
			$signature = sha1($str);
			
			$arr['appId'] = $this->wechat()->appid;
			$arr['timestamp'] = $time;
			$arr['noncestr'] = $noncestr;
			$arr['signature'] = $signature;
			$arr['base_url'] = $url;
			$arr['code'] = 'success';
		}
		else 
		{
			$arr['code'] = 'failed';
		}
		
		echo Util::jsonEncode($arr);
		exit;
		
		
// 		$arr = $this->configJsApi();
// 		echo Util::jsonEncode($arr);
// 		exit;
	}
}

